import React from 'react';
import { Button, Modal, Table } from 'antd';
import ContextModal from '@/utils/contextModal';
import { doChatRecords } from '@/services/datastatistics';
import moment from 'moment';
import { getSessionList } from '@/services/capacityStudy';

class SessionRecordModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      list: [],
      loading: false,
      contextMessage: [],
      contextModalVisible: false,
      row: {},
      current: 1,
      size: 9999,
      queryData: {},
      total: 0,
      contextRobotName: ''
    }
  }

  initQuery = (obj, row) => {
    this.setState({ visible: true, row, queryData: obj }, () => this.getDataList());
  }

  getDataList = () => {
    this.setState({ loading: true, }, async () => {
      const { queryData, current, size } = this.state;
      queryData.current = current;
      queryData.size = size;
      const res = await getSessionList(queryData)
      if (res.records && res.records.length === 1) {
        this.getChatRecords(res.records[0])
      };
      this.setState({ list: res.records ? res.records : [], total: res.total, loading: false })
    })
  }

  cancelModal = () => {
    this.setState({ dataSource: [], visible: false })
  }

  /**
   * 获取上下文
   * @returns {*}
   */
  getChatRecords = async row => {
    let res = await doChatRecords({
      sessionId: row.sessionId,
      time: moment(row.createTime).format('YYYY-MM-DD HH:mm:ss'),
      robotId: row.robotId,
    });
    this.setState({ contextMessage: res, contextModalVisible: true, contextRobotName: row.robotName });
  };

  cancelContextModal = () => {
    this.setState({ contextModalVisible: false, contextMessage: [], contextRobotName: '' });
  }

  render() {
    const { visible, list, loading, contextModalVisible, contextMessage, contextRobotName } = this.state;
    const columns = [
      {
        title: '用户名',
        dataIndex: 'username',
        align: 'center',
        width: 140,
      },
      {
        title: '发起时间',
        dataIndex: 'createTime',
        align: 'center',
        render: dataIndex => {
          return moment(dataIndex).format('YYYY-MM-DD HH:mm:ss');
        },
      },
      {
        title: '机器人',
        dataIndex: 'robotName',
        align: 'center',
        width: 100,
      },
      {
        title: '操作',
        align: 'center',
        render: (txt, row) => {
          return <Button type={'link'} onClick={() => this.getChatRecords(row)}>会话记录</Button>
        }
      }
    ];

    return (
      <div>
        <Modal title={'记录列表'} visible={visible} footer={null} width={800} onCancel={this.cancelModal}>
          <Table bordered columns={columns} dataSource={list} loading={loading} rowKey={(record, index) => index} pagination={false} />
        </Modal>

        <ContextModal
          visible={contextModalVisible}
          message={contextMessage}
          cancelContextModal={this.cancelContextModal}
          pageSource={'learn'}
          contextRobotName={contextRobotName}
        />
      </div>
    );
  }
}

export default SessionRecordModal;
